using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_Controls_RolesList : IWS.Lib.Utilities.Web.ListControl
{
	protected void Page_Load(object sender, EventArgs e)
	{
		this.grdRoles.RowCommand += new GridViewCommandEventHandler(grdRoles_RowCommand);

		if (!Page.IsPostBack)
		{
			pnlNew.Visible = false;
		}
	}

	void grdRoles_RowCommand(object sender, GridViewCommandEventArgs e)
	{
		switch (e.CommandName)
		{
			case "Remove" :
				int ix = Convert.ToInt32(e.CommandArgument);
				Label lblID = this.grdRoles.Rows[ix].FindControl("lblID") as Label;
				if (lblID != null)
				{
					//remove this role. 
					string[] users = System.Web.Security.Roles.Provider.GetUsersInRole(lblID.Text);
					if (users != null && users.Length > 0)
						System.Web.Security.Roles.Provider.RemoveUsersFromRoles(System.Web.Security.Roles.Provider.GetUsersInRole(lblID.Text),new string[] {lblID.Text});
					System.Web.Security.Roles.Provider.DeleteRole(lblID.Text, false);
				}
				break;
		}
		this.LoadRoles();
	}

	public void LoadRoles()
	{
		string[] roles = System.Web.Security.Roles.GetAllRoles();
		this.grdRoles.DataSource = roles;
		this.grdRoles.DataBind();
	}

	protected override void OnNewItemClick(object sender, EventArgs e)
	{
		pnlNew.Visible = true;
		this.txtName.Text = string.Empty;
	}
	protected void cmdSave_Click(object sender, EventArgs e)
	{
		pnlNew.Visible = false;
		System.Web.Security.Roles.Provider.CreateRole(txtName.Text);
		this.LoadRoles();
	}
}
